package QuizPackage;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

import javax.servlet.http.HttpServletRequest;

public class MultiAnswer extends Question{

	private String question;
	private String[] responses;
	private int numResponses;
	private boolean casesensitive;
	private boolean ordered = false;

	public MultiAnswer(int ID, ResultSet rs) {
		super(ID);
		try {
			question = rs.getString("question");
			casesensitive = Integer.parseInt(rs.getString("casesensitive")) == 1;
			StringTokenizer responseTokens = new StringTokenizer(rs.getString("response"),"|");
			numResponses = responseTokens.countTokens();
			responses = new String[numResponses];

			for (int i=0; i<numResponses; i++) {
				responses[i] = responseTokens.nextToken();
			}

			if (Integer.parseInt(rs.getString("ordered")) > 0){
				ordered = true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public String getHTML() {
		StringBuilder html = new StringBuilder();
		html.append("<p>" + question+"</p>\n");
		for (int i=0; i<numResponses; i++) {
			html.append("<input name=\""+questionID+"\" type=\"text\"> </br> \n");
		}
		String result = new String(html);
		return result;
	}

	@Override
	public int checkAnswer(String[] params) {
		//		for (int i=0; i<params.length; i++) ////System.out.println(params[i]);
		/*if (params.length != responses.length) {
			return 0;
		}*/
		int correct = 0;
		if (ordered) {
			for (int i=0; i<responses.length; i++) {
				StringTokenizer responseTokenizer = new StringTokenizer(responses[i],"`");
				while(responseTokenizer.hasMoreTokens())
				{
					if(!casesensitive && responseTokenizer.nextToken().compareToIgnoreCase(params[i])==0)
					{
						correct++;
						break;
					}
					if(casesensitive && responseTokenizer.nextToken().equals(params[i]))
					{
						correct++;
						break;
					}
				}
			}
			return correct;
		} else {
			HashSet<Integer> answeredResponses = new HashSet<Integer>();
			for(int i=0 ; i < params.length ; i++)
			{
				for(int j=0 ; j<responses.length ; j++)
				{
					boolean matched = false;
					if(!answeredResponses.contains(j))
					{
						StringTokenizer responseTokenizer = new StringTokenizer(responses[j],"`");
						while(responseTokenizer.hasMoreTokens())
						{
							if(!casesensitive && responseTokenizer.nextToken().compareToIgnoreCase(params[i])==0)
							{
								correct++;
								answeredResponses.add(j);
								matched = true;
								break;
							}
							if(casesensitive && responseTokenizer.nextToken().equals(params[i]))
							{
								correct++;
								answeredResponses.add(j);
								matched = true;
								break;
							}
						}
					}
					if(matched) break;
				}
			}
			return correct;
		}
	}

	@Override
	public int totalPoints() {
		return responses.length;
	}

	@Override
	public String correctAnswer() {
		String answer = "";
		for (int i=0; i<responses.length; i++) {
			StringTokenizer responseTokenizer = new StringTokenizer(responses[i],"`");
			answer += responseTokenizer.nextToken() + "\n";
		}
		return answer;
	}

	public String toString() {
		return question;
	}

	public static String createQHTML(int numResponses) {
		StringBuilder html = new StringBuilder();
		html.append("<p>Enter the question.</p>");
		html.append("<input name=\"question\" type=\"text\">");
		for(int i =0; i < numResponses ; i++)
		{
			html.append("<p>Enter the preffered response for answer #"+i+"(Do not use | or ` in your responses please).</p>");
			html.append("<input name=\"preffered"+i+"\" type=\"text\">");
			html.append("<p>Enter up to 4 additional responses for answer# "+(i+1)+"(Different forms of the answer, ex. 1 and one).</p>");
			html.append("<input name=\"r1"+i+"\" type=\"text\">");
			html.append("<input name=\"r2"+i+"\" type=\"text\">");
			html.append("<input name=\"r3"+i+"\" type=\"text\">");
			html.append("<input name=\"r4"+i+"\" type=\"text\">");
		}

		html.append("<input name=\"ordered\" type=\"radio\" value=\"true\"/> Ordered </>");
		html.append("<input name=\"ordered\" type=\"radio\" value=\"false\"/ checked> Unordered </>");

		html.append("<p>Should this question be case-sensitive?</p>");
		html.append("<input name=\"casesensitive\" type=\"radio\" value=\"false\" checked>No");
		html.append("<input name=\"casesensitive\" type=\"radio\" value=\"true\">Yes");
		
		String result = new String(html);
		return result;
	}

	public static void submitQuestion(int quizid, HttpServletRequest request) {
		QuizDBC dbc = new QuizDBC();
		String question = request.getParameter("question");
		//String response = request.getParameter("preferred");
		int order;
		////System.out.println("order:" + request.getParameter("ordered"));
		String orderStr = (String) request.getParameter("ordered");
		if (orderStr.equals("true")) {
			order = 1;
		} else {
			order = 0;
		}

		int casesensitive = (request.getParameter("casesensitive").equals("false")) ? 0 : 1;
		
		int numResponses = Integer.parseInt(request.getParameter("responses"));
		StringBuilder response = new StringBuilder();

		for(int i=0 ; i < numResponses ; i++)
		{
			response.append(request.getParameter("preffered"+i));
			for(int j=1 ; j<=4 ; j++)
			{
				if(!request.getParameter("r"+j+i).equals("")) response.append("`"+request.getParameter("r"+j+i));
			}
			if(i != (numResponses-1)) response.append("|");
		}

		String resp = new String(response.toString());

		dbc.editTable(String.format("INSERT INTO multipleresponse VALUES(\"%s\", \""+resp+"\", %d, %d,%d);", question, order, quizid,casesensitive));
	}
}
